layui.define(function (exports) {

    var a = layui.jquery,
        admin_map = a("#admin_map").val(),
        merchant_id = a("#merchant_id").val(),
        {createEditor, createToolbar} = window.wangEditor,
        T = {
            init: function () {
                a('.wang-editor').each(function () {
                    const filed = a(this).attr('data-filed');
                    const value = a(this).attr('data-value');
                    const editorConfig = {
                        placeholder: '在此处填写内容...',
                        filed: filed,
                        MENU_CONF: {},
                        onCreated(editor) {
                            const html = editor.getHtml()
                            a(".editor—" + filed).val(html === "<p><br></p>" ? "" : html)
                        },
                        onChange(editor) {
                            const html = editor.getHtml()
                            a(".editor—" + filed).val(html === "<p><br></p>" ? "" : html)
                        }
                    }
                    editorConfig.MENU_CONF['uploadImage'] = {

                        server: a("#uploads_url").val(),
                        fieldName: 'file',
                        // 单个文件的最大体积限制，默认为 2M
                        maxFileSize: 5 * 1024 * 1024, // 1M
                        // 选择文件时的类型限制，默认为 ['image/*'] 。如不想限制，则设置为 []
                        allowedFileTypes: ['image/*'],
                        customInsert(res, insertFn) {
                            const imgInfo = res.data || {}
                            const { url, alt, href } = imgInfo
                            if (!url) throw new Error(`图片地址为空`)
                            insertFn(url, alt, href)
                        },
                    
                    }
                    editorConfig.MENU_CONF['uploadVideo'] = {
                        // 上传视频的配置
                        server: a("#uploadEditorVideo").val(),
                        maxFileSize: 50 * 1024 * 1024, // 5M
                        maxNumberOfFiles: 3,
                        allowedFileTypes: ['video/*'],    
                        customInsert(res, insertFn) {
                          const videoInfo = res.data || {}
                          const { url, poster } = videoInfo
                          if (!url) throw new Error(`视频地址为空`)
                          insertFn(url, poster)
                        },
                    }

                    const editor = createEditor({
                        selector: '#editor-container-' + filed,
                        html: value,
                        config: editorConfig,
                        mode: 'default', // or 'simple'
                    })
                    const toolbar = createToolbar({
                        editor,
                        selector: '#toolbar-container-' + filed,
                        // config: toolbarConfig,
                        mode: 'default', // or 'simple'
                    })
                })
            }
        }
    exports('wangEditor', T);
});